package com.bdqn.tour.servlet.Hqf;

import com.alibaba.fastjson.JSONArray;
import com.bdqn.tour.pojo.Scenic;
import com.bdqn.tour.service.HqfqueryService;
import com.bdqn.tour.service.impl.hqfqueryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/querycity")
//查询城市景点所有内容
public class HqfqueryAllservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //设置编码格式
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //获取参数
        String city = req.getParameter("city");
        //调用业务层 hqfqueryService数据
        HqfqueryService q = new hqfqueryServiceImpl();
        //获取所有城市数据加入Scenic集合中
        List<Scenic> scenicList = q.getScenicList(city);
        //将景点列表转换为json格式
        String jsonResponse = JSONArray.toJSONString(scenicList);
        //通过printwriter将json字符串写入http响应中
        resp.setContentType("application/json");
        PrintWriter out = resp.getWriter();
        out.print(jsonResponse);
        out.flush();


    }
}
